<form action="#" class="layui-form">
    <div class="layui-form-item">
        <!--        <div class="layui-inline">-->
        <!--            <label>名称:</label>-->
        <!--            <div class="layui-inline">-->
        <!--                <input type="text" name="name" autocomplete="off" placeholder="请输入名称"-->
        <!--                       class="layui-input">-->
        <!--            </div>-->
        <!--            状态:-->
        <!--            <div class="layui-inline">-->
        <!--                <select name="state">-->
        <!--                    <option value="">请选择</option>-->
        <!--                    <option value="0">无效</option>-->
        <!--                    <option value="1">有效</option>-->
        <!--                </select>-->
        <!--            </div>-->
        <!--            <button class="layui-btn" lay-submit lay-filter="recharge-table">搜索</button>-->
        <!--        </div>-->
    </div>
</form>
<script type="text/html" id="recharge-head-bar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="goEdit"><i class="layui-icon">&#xe654;</i>添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除
        </button>
    </div>
</script>
<script type="text/html" id="recharge-recharge-bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit-recharge">编辑</a>
</script>
<table class="layui-table" lay-filter="recharge-table" id="recharge-table">
</table>
<script type="text/javascript">
    layui.use(['table', 'layer', 'form'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        form.render();
        table.render({
            id: "recharge-table",
            elem: '#recharge-table'
            , toolbar: '#recharge-head-bar'
            , cellMinWidth: 80
            , url: '/recharge/table' //数据接口
            , page: true //开启分页
            , cols: [[
                {checkbox: true},
                {field: 'id', title: 'ID', sort: true}
                , {field: 'cusid', title: '客户id'}
                , {field: 'orderid', title: '订单号'}
                , {field: 'createtime', title: '创建时间'}
                , {field: 'updatetime', title: '更新时间'}
                , {field: 'paymenttype', title: '支付类型', templet: function (data) {
                        if (data.paymenttype == 1) {
                            return '<span class="layui-badge layui-bg-green">微信支付</span>'
                        } else if (data.paymenttype == 0) {
                            return '<span class="layui-badge layui-bg-green">银联支付</span>'
                        } else if (data.paymenttype == 2) {
                            return '<span class="layui-badge layui-bg-green">支付宝</span>'
                        }
                    }}
                , {
                    field: 'money', title: '金额(元)', templet: function (data) {
                        return data.money/10000;
                    }
                }
                , {
                    field: 'state', title: '状态', templet: function (data) {
                        if (data.state == 1) {
                            return '<span class="layui-badge layui-bg-green">已支付</span>'
                        } else if (data.state == 0) {
                            return '<span class="layui-badge layui-bg-green">未支付</span>'
                        } else if (data.state == 2) {
                            return '<span class="layui-badge layui-bg-green">已关闭</span>'
                        }
                    }
                }
                , {fixed: 'right', title: '操作', toolbar: '#recharge-recharge-bar', width: 180}
            ]]
        });
        //头工具栏事件
        table.on('toolbar(recharge-table)', function (obj) {
            switch (obj.event) {
                case 'goEdit':
                    openEditWindow(null);
                    break;
                case 'delete':
                    var data = table.checkStatus('recharge-table').data;
                    if (data.length > 0) {
                        layer.confirm('真的删除行么', function (index) {
                            var param = ""
                            $.each(data, function (i, obj) {
                                param += "&ids=" + obj.id
                            })
                            $.ajax({
                                url: '/recharge/del',
                                data: param,
                                method: 'post',
                                success: function (result) {
                                    if (result.status) {
                                        table.reload('recharge-table', {});
                                    } else {
                                        alert(result.msg)
                                    }
                                    layer.close(index);
                                }
                            })
                        });
                    }
                    break;
            }
            ;
            ;
        });
        //监听行工具事件
        table.on('tool(recharge-table)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit-recharge': {
                    openEditWindow(data);
                    break;
                }

            }
        });

        function openEditWindow(data) {
            layer.open({
                type: 1,
                content:  $('#recharge-edit-layer').html(),
                title: '编辑'
                , area: ['500px', '400px'],
                btn: ['提交', '取消'] //可以无限个按钮
                , yes: function (index, layero) {
                    layer.load()
                    $.ajax({
                        url: "/recharge/" + (data == null ? 'add' : 'update'),
                        //  url: '/sys/recharge/update',
                        data:  $("#recharge-edit-form").serialize(),
                        method: 'post',
                        success: function (result) {
                            if (result.status) {
                                table.reload('recharge-table', {});
                                layer.close(index);
                            } else {
                                alert(result.msg)
                            }
                            layer.closeAll('loading');
                        }
                    })
                }, success: function (layero, index) {
                    form.render()
                    if (data != null) {
                        form.val("recharge-edit-form", data);
                        form.val("recharge-edit-form", {
                            "state": data.state + ""
                        });
                    }
                    $.ajax({
                        url: "sys/customer/tree",
                        method: "post",
                        success: function (res) {

                            for (var i = 0; i < res.length; i++) {
                                if (data&&data.cusId&&data.cusId == res[i].id) {
                                    $("#customerId").append('<option selected value="' + res[i].id + '">' + res[i].nickname + '</option>');
                                }else{
                                    $("#customerId").append('<option value="' + res[i].id + '">' + res[i].nickname + '</option>');

                                }
                            }
                            form.render();//后追加到页面的元素，layui不渲染，必须render
                        }
                    })
                }
            });
        }

        form.on('submit(recharge-table)', function (data) {
            table.reload('recharge-table', {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: data.field
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

    })
    ;

</script>

<script type="text/html" id="recharge-edit-layer">
    <form id="recharge-edit-form" style="width: 80%" class="layui-form" lay-filter="recharge-edit-form">
        <input type="hidden" name="id">

        <div class="layui-form-item">
            <label class="layui-form-label">选择所属客户</label>
            <div class="layui-input-inline">
                <select name="cusid" id="customerId">
                    <option value="0">请选择</option>
                    <option value="1" selected>一级</option>
                    <option value="2">二级</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">金额(元)</label>
            <div class="layui-input-block">
                <input type="number" name="money" required lay-verify="required" placeholder="请输入金额"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select id="state" name="state">
                    <option value="0">未支付</option>
                    <option value="1" selected>已支付</option>
                    <option value="2">已关闭</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">支付方式</label>
            <div class="layui-input-block">
                <select id="paymenttype" name="paymenttype">
                    <option value="0">银联</option>
                    <option value="1" selected>微信</option>
                    <option value="2">支付宝</option>
                </select>
            </div>
        </div>
    </form>
</script>

</script>